package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.check.CheckAddReqDTO;
import com.yixin.hospital.entity.dto.req.check.WriteOffReqDTO;
import com.yixin.hospital.mapper.extend.CheckRepositoryExtendMapper;
import com.yixin.hospital.mapper.extend.DrugDepositoryDrugExtendMapper;
import com.yixin.hospital.service.CheckDetailService;
import com.yixin.hospital.service.CheckRepositoryService;
import com.yixin.hospital.service.DrugDepositoryDrugService;
import com.yixin.hospital.service.DrugDepositoryService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/check-repository")
@Api(value = "盘点管理", tags = "盘点管理")
public class CheckRepositoryController {

    @Resource
    private CheckRepositoryService checkService;
    @Resource
    private CheckDetailService checkDetailService;
    @Resource
    private CheckRepositoryService checkRepositoryService;
    @Resource
    private CheckRepositoryExtendMapper checkRepositoryExtendMapper;
    @Resource
    private DrugDepositoryService drugDepositoryService;
    @Resource
    private DrugDepositoryAuthController drugDepositoryAuthController;
    @Resource
    private DrugDepositoryDrugService drugDepositoryDrugService;
    @Resource
    private DrugDepositoryDrugExtendMapper drugDepositoryDrugExtendMapper;

    @GetMapping("/list")
    @ApiOperation(value = "盘点记录列表")
    public ApiResult<?> list(@RequestParam int currentPage, @RequestParam int pageSize, Long drugDepositoryId, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return checkRepositoryService.allList(currentPage, pageSize, drugDepositoryId, userId);
    }

    @GetMapping("/info")
    @ApiOperation(value = "盘点详情")
    public ApiResult<?> info(@RequestParam int currentPage, @RequestParam int pageSize, @RequestParam int checkId) {
        return checkRepositoryService.getInfo(currentPage, pageSize, checkId);
    }

    @PostMapping("/addOrUpdate")
    @Transactional
    @ApiOperation(value = "添加盘点")
    public ApiResult<?> addOrUpdate(@Valid @RequestBody CheckAddReqDTO checkAddReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return checkRepositoryService.addOrUpdate(checkAddReqDTO, userId, userName);
    }


    @GetMapping("/writeOffList")
    @ApiOperation(value = "核销记录列表")
    public ApiResult<?> writeOffList(@RequestParam int currentPage, @RequestParam int pageSize, Long drugDepositoryId, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return checkRepositoryService.writeOffList(currentPage, pageSize, drugDepositoryId, userId);
    }

    @PostMapping("/writeOff")
    @Transactional
    @ApiOperation(value = "核销")
    public ApiResult<?> writeOff(@Valid @RequestBody WriteOffReqDTO writeOffReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));

        return checkRepositoryService.writeOff(writeOffReqDTO, userId, userName);
    }

    @PostMapping("/writeOff-remarks")
    @Transactional
    @ApiOperation(value = "核销单建议")
    public ApiResult<?> writeOffRemarks(@Valid @RequestBody WriteOffReqDTO writeOffReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return checkRepositoryService.writeOffRemarks(writeOffReqDTO, userId, userName);
    }

    @GetMapping("/depository-num-list")
    @ApiOperation(value = "根据药品Id 库房id 查库存列表")
    public ApiResult<?> queryDepositoryNumList(@RequestParam int currentPage, @RequestParam int pageSize,
                                               @RequestParam(value = "drugDepositoryId") Long drugDepositoryId, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return checkRepositoryService.queryDepositoryNumList(currentPage, pageSize, drugDepositoryId, userId);
    }


}
